//
//  ViewController.m
//  UI15-UICollectionVIewLifeCycle
//
//  Created by 韩旭 on 16/8/3.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionCell.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

#define WIDETH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 5) / 2, 100);
    layout.scrollDirection = 0;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    [_collectionView registerClass:[MyCollectionCell class] forCellWithReuseIdentifier:@"ce"];
    [self.view addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //    1.   numberOfSectionsInCollectionView
    //    2.collectionView：numberOfItemsInSection
    //    注意3-5循环执行
    //    3.prepareForReuse（从重用池取出时调用）
    //   4. collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    //    5.- (void)collectionView:collectionView willDisplayCell:cell forItemAtIndexPath:indexPath
    //    当cell 完全离开屏幕时
    //  6.  collectionView:collectionView didEndDisplayingCell:cell forItemAtIndexPath:indexPath
//    iOS10以前
//    出屏目立即返回, 依旧进入3->4->5
//    iOS 10 以后
//    出屏幕立即返回， 会执行4->5 , 不会执行3，提高了效率
    
}
#pragma mark - collectionView LifeCycle 生命周期
// 区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"11111%@ : %s", self,__func__);
    return 1;
}

// 区里对应的行数cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"222222%@: --- %s", self, __func__);
    return 20;
}
//从重用池中区出cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ce" forIndexPath:indexPath];
    NSLog(@"555%@", self);
    cell.backgroundColor = COLORAUTO;
    return cell;
}
// IOS8.0以后推出，cell将要显示在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"333333%@- - %ld", cell, indexPath.row);
}
// IOS8.0以后推出，cell已经离开在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSLog(@"4444444%@ -- %ld", cell ,indexPath.row);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end